﻿<template>
  <div class="qMain_div">

    <div class="content" :class="{'sidebar_child_heddin':!childMenuShow,sidebarSmall:sidebarSmall}">
      <div class="sidebar" v-if="$store.state.routerName !== '选择公司'">
        <div class="logo showLogo" :class="{'logo_s': sidebarSmall}"><img src="/static/imgs/logo_normal.png" alt=""/></div>
        <div class="sidebar-fold" @click="$store.state.sidebarSmall = !$store.state.sidebarSmall">
          <i class="iconfont" :class="{'icon-sanhengxian':!sidebarSmall,'icon-sanhengxian-copy':sidebarSmall}"></i>
        </div>
        <ul>
          <li :class="{menuActive:$store.state.path[0] === 'navigation'}">
            <router-link to="/manager/navigation">
              <i class="iconfont icon-shouye-shouye"></i>
              <span class="sidebar-text">首页</span>
              <!--<span class="fa fa-angle-right"></span> -->
            </router-link>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'sale'}" v-if="['ROLE_ADMIN','ROLE_SALES','ROLE_FINANCE'].hasMap($store.state.userInfo.roleNameStr)">
            <a>
              <i class="iconfont icon-xiaoshou"></i>
              <span class="sidebar-text">销售</span>
              <span class="fa fa-angle-right"></span>

            </a>
            <div class="sidebar_child">
              <child-menu :menuData="pathData.sale.children" :menuTitle="pathData.sale.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'depot'}" v-if="['ROLE_ADMIN','ROLE_WAREHOUSE','ROLE_FINANCE'].hasMap($store.state.userInfo.roleNameStr)">
            <a>
              <i class="iconfont icon-ccgl-shiticangku-9"></i>
              <span class="sidebar-text">仓库</span>
              <span class="fa fa-angle-right"></span>
            </a>
            <div class="sidebar_child">
              <child-menu :menuData="pathData.depot.children" :menuTitle="pathData.depot.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'finance'}"  v-if="['ROLE_ADMIN','ROLE_FINANCE'].hasMap($store.state.userInfo.roleNameStr)">
            <a>
              <i class="iconfont icon-caiwu"></i>
              <span class="sidebar-text">财务</span>
              <span class="fa fa-angle-right"></span>
            </a>

            <div class="sidebar_child">
              <child-menu :menuData="pathData.finance.children" :menuTitle="pathData.finance.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'cost'}" v-if="['ROLE_ADMIN','ROLE_FINANCE'].hasMap($store.state.userInfo.roleNameStr)">
            <a>
              <i class="iconfont icon-feiyongguanli"></i>
              <span class="sidebar-text">费用</span>
              <span class="fa fa-angle-right"></span>
            </a>

            <div class="sidebar_child">
              <child-menu :menuData="pathData.cost.children" :menuTitle="pathData.cost.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'payments'}" v-if="['ROLE_ADMIN','ROLE_FINANCE'].hasMap($store.state.userInfo.roleNameStr)">
            <a>
              <i class="iconfont icon-daifukuan"></i>
              <span class="sidebar-text">收付款</span>
              <span class="fa fa-angle-right"></span>
            </a>
            <div class="sidebar_child">
              <child-menu :menuData="pathData.payments.children" :menuTitle="pathData.payments.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'analysis'}" v-if="[].hasMap($store.state.userInfo.roleNameStr)">
            <a>
              <i class="iconfont icon-shujufenxi1"></i>
              <span class="sidebar-text">分析</span>
              <span class="fa fa-angle-right"></span>
            </a>
            <div class="sidebar_child">
              <child-menu :menuData="pathData.analysis.children" :menuTitle="pathData.analysis.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'tax'}" v-if="">
            <a>
              <i class="iconfont icon-shui1"></i>
              <span class="sidebar-text">税务</span>
              <span class="fa fa-angle-right"></span>
            </a>
            <div class="sidebar_child">
              <child-menu :menuData="pathData.tax.children" :menuTitle="pathData.tax.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li :class="{menuActive:$store.state.path[0] === 'jinding'}" v-if="true" >
            <a>
              <i class="iconfont icon-shui1"></i>
              <span class="sidebar-text">金鼎</span>
              <span class="fa fa-angle-right"></span>
            </a>
            <div class="sidebar_child">
              <child-menu :menuData="pathData.jinding.children" :menuTitle="pathData.jinding.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li class="sidebar_setting" style="margin-bottom:50px;" :class="{menuActive:$store.state.path[0] === 'options'}" v-if="['ROLE_ADMIN','ROLE_SALES','ROLE_FINANCE'].hasMap($store.state.userInfo.roleNameStr)">
            <a>
              <i class="iconfont icon-shezhi"></i>
              <span class="sidebar-text">设置</span>
              <span class="fa fa-angle-right"></span>
            </a>
            <div class="sidebar_child sidebar_child_top">
              <child-menu :menuData="pathData.options.children" :menuTitle="pathData.options.name" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>
          <li>
            <a>
              <i class="iconfont icon-shoucang"></i>
              <span class="sidebar-text">收藏</span>
              <span class="fa fa-angle-right"></span>
            </a>
          </li>
          <li>
            <a>
              <i class="iconfont icon-shizhong"></i>
              <span class="sidebar-text">最近</span>
              <span class="fa fa-angle-right"></span>
            </a>
            <div class="sidebar_child sidebar_child_top" v-show="latelyPath.length">
              <child-menu :menuData="latelyPath" :path="path" @showChange="showChange"></child-menu>
            </div>
          </li>

        </ul>
      </div>
      <div class="sidebar_child" v-if="false">
        <child-menu :menuData="menu" :path="path" @showChange="showChange"></child-menu>
      </div>
      <div class="workspace" :class="{hidenSidebar: $store.state.routerName == '选择公司'}">
        <slot name="content"></slot>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    pathData
  } from '@/assets/optionsData'

  import {
    mapState,
    mapActions
  } from 'vuex'
  export default {
    props: {

    },
    components: {

    },
    data: function() {
      return {
        breadcrumb: [],
        menu: [],
        menuActive: '',
        path: [],
        childMenuShow: true,
        pathData: pathData,
        latelyPath: []
      }
    },
    computed: {
      sidebarSmall: function() {
        return this.$store.state.sidebarSmall
      },
      smallShowSearch: function() {
        return this.$store.state.smallShowSearch
      }
    },
    created() {
      var that = this;
      initPath()
      this.$router.afterEach(function(to,from) {
        if (window.screen.width <= 1440) {
          console.log(to.path);
          that.$store.state.sidebarSmall = to.path !== '/manager/navigation';
        }
        initPath()
        that.$store.state.fromPath = from.path.split('/');
        that.$nextTick(function() {
          // DOM 更新了

          that.setTbaleHeiht();
        })

      })

      function initPath() {
        var path = that.$route.path.split('/')

        path.splice(0, 2);
        path = path.map(function(item) {
          return item
        })
        var pathArr = []
        var i = 0;

        function getPath(pathData) {
          if (pathData[path[i]]) {
            pathArr.push(pathData[path[i]])
            if (pathData[path[i]].children) {
              i++
              getPath(pathData[path[i - 1]].children)
            }
          }
          return pathArr
        }
        getPath(pathData);
        that.breadcrumb = pathArr;
        var menu = {};
        if (path.length === 2) {
          menu = pathData[path[0]].children || {};
        } else if (path.length === 3) {
          menu = pathData[path[0]].children || {};
          menu = menu[path[1]].children || {};
        }

        that.menu = menu
        that.$store.state.menu = menu;
        that.path = path;
        that.$store.state.path = path;
        that.$store.state.routerName = pathArr[pathArr.length - 1].name;
        that.latelyPath = setLatelyPath(pathArr[pathArr.length - 1]);
        that.menuActive = path[path.length - 1]
      }

      function setLatelyPath(obj) {
        var LatelyPath = localStorage.getItem("latelyPath")
        if (LatelyPath) {
          LatelyPath = JSON.parse(localStorage.getItem("latelyPath"));
          if (obj.url === "/manager/navigation") {
            return LatelyPath
          }
          var length = LatelyPath.length
          var flag = false;
          for (var i = 0; i < length; i++) {

            if (LatelyPath[i].url == obj.url) {
              var oldObj = LatelyPath[i]
              LatelyPath.splice(i, 1);
              LatelyPath.unshift(oldObj);
              flag = true;
            }
          }
          if (!flag) {
            LatelyPath.unshift(obj);
            if (LatelyPath.length === 6) {
              LatelyPath.pop();
            }
          }
          localStorage.setItem("latelyPath", JSON.stringify(LatelyPath));
        } else {
          LatelyPath = [];
          if (obj.url === "/manager/navigation") {
            return LatelyPath
          }
          LatelyPath.push(obj);
          localStorage.setItem("latelyPath", JSON.stringify(LatelyPath));
        }
        return LatelyPath;
      }
      //设置最近访问菜单列表
      // function setLatelyPath(obj) {
      //   var LatelyPath = localStorage.getItem("latelyPath");

      //   if (LatelyPath) {
      //     LatelyPath = JSON.parse(LatelyPath);
      //     var length = LatelyPath.length;
      //     var flag = false;
      //     for (var i = 0; i < length; i++) {
      //       if (LatelyPath[i].url == obj.url) {
      //         var oldObj = LatelyPath[i]
      //         oldObj.count++;
      //         flag = true;
      //       }
      //     }
      //     if(!flag){
      //       obj.count = 1;
      //       LatelyPath.push(obj);
      //     }
      //     localStorage.setItem("latelyPath", JSON.stringify(LatelyPath));
      //   } else {
      //     LatelyPath = [];
      //     obj.count = 1;
      //     LatelyPath.push(obj);
      //     localStorage.setItem("latelyPath", JSON.stringify(LatelyPath));
      //   }
      //   console.log(LatelyPath)
      //   for(var c = 0 ; c < LatelyPath.length; c++){
      //     for(var j = 0 ; j < LatelyPath.length - c - 1 ; j++){
      //       if(LatelyPath[j].count < LatelyPath[j+1].count){
      //         var temp = LatelyPath[j];
      //         LatelyPath[j] = LatelyPath[j+1];
      //         LatelyPath[j+1] = temp;
      //       }

      //     }
      //   }
      //   return LatelyPath.slice(0,5);
      // }

    },
    mounted() {
      var  that = this;
      this.$el.getElementsByClassName('workspace')[0].addEventListener('mouseenter',function(e){
        if(that.$store.state.smallPattern){
          that.$store.state.smallShowSearch = false;
        }
      },false);
      if (window.screen.width <= 1440) {
        this.$store.state.sidebarSmall =  this.$route.path !== '/manager/navigation';
      }

      let canvas = document.createElement('canvas'),
          tips = document.createElement('div'),
          clientWidth = document.body.clientWidth,
          clientHeight = document.body.clientHeight,
          ctx = canvas.getContext('2d'),
          hasStart = false,
          startX = 0,
          startY = 0,
          endX = 0,
          endY = 0;

      // console.log(canvas);
      // console.log(clientWidth, clientHeight);
      tips.setAttribute('id', 'canvasTips');
      tips.setAttribute("style", "position:fixed;background-color:#ffffff;padding: 20px;");

      canvas.setAttribute('id', 'editCanvas');
      canvas.setAttribute('width', clientWidth);
      canvas.setAttribute('height', clientHeight);

      canvas.addEventListener('mousedown', function(e){
        hasStart = true;
        tips.style.display = 'none';
        startX = e.clientX,
        startY = e.clientY;

        console.log('mousedown');
        console.log(startX, startY);

      });

      let countColumn = function(){
        let tds = [...document.getElementsByClassName('canvas-count')],
            total = [];
        tds.forEach(el => {
          let rect = el.getBoundingClientRect();
          if (
              rect && 
              (startX < rect.left && rect.left < endX) && 
              (startY < rect.top && rect.top < endY)
            ) {
            let s = el.innerText.replace(/[\,\r\n]/gi, ''),
                number = Number(s);

            !isNaN(number) && total.push(number);
          }
          // console.log(el.getAttribute('class'));
          // console.log(el.offsetLeft, el.offsetTop);
          // console.log(el.innerText);
        });
        tips.style.left = endX + 'px';
        tips.style.top = endY + 'px';
        tips.style.display = 'block';
        tips.innerText = total.reduce(function(sum, value) {
          return sum + value;
        }, 0) + '元';
        console.log(total);
      }

      canvas.addEventListener('mousemove', function(e){
        if (!hasStart) { return }
        if (canvas.getContext){
          ctx.clearRect(0, 0, clientWidth, clientHeight);
        }
        endX = e.clientX,
        endY = e.clientY;

        console.log('mousemove');
        console.log(endX, endY);

        if (canvas.getContext){
          ctx.strokeRect(startX, startY, (endX - startX), (endY - startY));
        }
        Lib.Utils.throttle(countColumn, this);
      });

      canvas.addEventListener('mouseup', function(e){
        hasStart = false;
      });

      document.getElementsByTagName('body').item(0).append(canvas);
      document.getElementsByTagName('body').item(0).append(tips);
    },
    watch:{
      smallShowSearch :function(){
        this.setTbaleHeiht();
      }
    },
    methods: {
      ...mapActions(['setTbaleHeiht']),
      activeTab(index) {
        that.$emit('activetab', index);
      },
      showChange(type) {
        this.childMenuShow = type;
      },
    }
  }
</script>


<style scoped>
  .sidebar {
    width: 126px;
    position: fixed;
    top:0;
    left:0;
    background: #3f4146;
    height: 100%;
    color: #fff;
    transition: all .3s;
    z-index:11;
  }

  .sidebarSmall .sidebar {
    width: 50px;
  }

  .sidebar li {
    width: 100%;
    height: 40px;
    line-height: 40px;
    position: relative;
    cursor: pointer;
    margin-top: 5px;
  }

  .sidebar li.menuActive {
    background: #2c2e33;
    border-left: 3px solid #bd0e1f;
    box-sizing: border-box;
  }

  .sidebar li.menuActive a {
    color: rgba(255, 255, 255, 1.0);
  }

  .sidebar li:hover {
    background-color: rgba(44, 46, 50, 0.8);
  }

  .sidebar li:hover .sidebar_child {
    display: block !important;
  }

  .sidebar li a {
    display: inline-block;
    height: 100%;
    width: 100%;
    color: rgba(255, 255, 255, 0.85);
    font-size: 14px;
    overflow: hidden;
  }

  .sidebar li:hover a {
    color: rgba(255, 255, 255, 1.0);
  }

  .sidebar li a i {
    display: inline-block;
    font-size: 20px;
    width: 50px;
    text-align: center;
    line-height: 40px;
    float: left;
  }

  .sidebar li.menuActive a i {
    margin-left: -3px;
  }

  .sidebar li a .sidebar-text {
    display: inline-block;
    width: 50px;
  }

  .sidebar-fold {
    width: 100%;
    text-align: center;
    height: 20px;
    line-height: 20px;
    background: #46484d;
    cursor: pointer;
  }

  .sidebar-fold i {
    font-size: 14px;
  }

  .content {
    position: relative;
    overflow: hidden;
  }

  .footer {
    border-top: 2px solid #840104;
    height: 40px;
  }

  .sidebar_lm_S {
    background: url(/static/imgs/mainframe/sidebarS.png)
  }

  .sidebar_lm_S_A {
    background: url(/static/imgs/mainframe/sidebarSA.png)
  }

  .sidebar_lm_K {
    background: url(/static/imgs/mainframe/sidebarK.png)
  }

  .sidebar_lm_K_A {
    background: url(/static/imgs/mainframe/sidebarKA.png)
  }

  .sidebar_lm_C {
    background: url(/static/imgs/mainframe/sidebarC.png)
  }

  .sidebar_lm_C_A {
    background: url(/static/imgs/mainframe/sidebarCA.png)
  }

  .sidebar_lm_B {
    background: url(/static/imgs/mainframe/sidebarB.png)
  }

  .sidebar_lm_B_A {
    background: url(/static/imgs/mainframe/sidebarBA.png)
  }

  .sidebar_lm_T {
    background: url(/static/imgs/mainframe/sidebarT.png)
  }

  .sidebar_lm_T_A {
    background: url(/static/imgs/mainframe/sidebarTA.png)
  }

  .sidebar_lm_L {
    background: url(/static/imgs/mainframe/sidebarL.png)
  }

  .sidebar_lm_L_A {
    background: url(/static/imgs/mainframe/sidebarLA.png)
  }

  .sidebar_small {
    width: 45px;
    position: absolute;
    background: #fff;
    height: 100%;
    z-index: 100
  }

  .sidebar_small li {
    width: 45px;
    height: 50px;
    overflow: hidden;
  }

  .sidebar_small li a {
    display: block;
    text-indent: -9999px;
    height: 50px;
  }

  .sidebar_lm_S_small {
    background: url(/static/imgs/mainframe/sidebarS_small.png)
  }

  .sidebar_lm_S_A_small {
    background: url(/static/imgs/mainframe/sidebarSA_small.png)
  }

  .sidebar_lm_K_small {
    background: url(/static/imgs/mainframe/sidebarK_small.png)
  }

  .sidebar_lm_K_A_small {
    background: url(/static/imgs/mainframe/sidebarKA_small.png)
  }

  .sidebar_lm_C_small {
    background: url(/static/imgs/mainframe/sidebarC_small.png)
  }

  .sidebar_lm_C_A_small {
    background: url(/static/imgs/mainframe/sidebarCA_small.png)
  }

  .sidebar_lm_B_small {
    background: url(/static/imgs/mainframe/sidebarB_small.png)
  }

  .sidebar_lm_B_A_small {
    background: url(/static/imgs/mainframe/sidebarBA_small.png)
  }

  .sidebar_lm_T_small {
    background: url(/static/imgs/mainframe/sidebarT_small.png)
  }

  .sidebar_lm_T_A_small {
    background: url(/static/imgs/mainframe/sidebarTA_small.png)
  }

  .sidebar_lm_L_small {
    background: url(/static/imgs/mainframe/sidebarL_small.png)
  }

  .sidebar_lm_L_A_small {
    background: url(/static/imgs/mainframe/sidebarLA_small.png)
  }


  /*.sidebar_lm_S_small:visited { background:url(/static/imgs/mainframe/sidebarSA_small.png)}
  .sidebar_lm_K_small:visited { background:url(/static/imgs/mainframe/sidebarKA_small.png)}
  .sidebar_lm_C_small:visited { background:url(/static/imgs/mainframe/sidebarCA_small.png)}
  .sidebar_lm_B_small:active { background:url(/static/imgs/mainframe/sidebarBA_small.png)}
  .sidebar_lm_T_small:active { background:url(/static/imgs/mainframe/sidebarTA_small.png)}
  .sidebar_lm_L_small:active { background:url(/static/imgs/mainframe/sidebarLA_small.png)} */

  .workspace {
    background: #F6F6F6;
    margin-left: 126px;
    padding-left: 16px;
    /*overflow-y: auto;*/
    transition: all .3s;
    display: flex;
    display: -ms-flexbox;
    display: -webkit-flex;
    box-orient: vertical;
    flex-direction: column;
    -ms-flex-direction: column;
    height: 100%;
  }

  .sidebarSmall .workspace {
    margin-left: 50px;
  }
  .hidenSidebar.workspace,sidebarSmall .hidenSidebar.workspace{
      margin-left: 0px;
  }
  .sidebar_child_heddin .workspace {
    margin-left: 45px;
  }

  .sidebar_child {
    display: none;
    position: absolute;
    top: 0;
    left: 126px;
    width: 180px;
    transition: all .3s;
    z-index: 100;
    opacity: 1.0;
  }

  .menuActive .sidebar_child {
    left: 123px;
  }

  .sidebarSmall .sidebar_child {
    left: 50px;
  }

  .sidebarSmall .menuActive .sidebar_child {
    left: 47px;
  }

  .sidebar_child_heddin .sidebar_child {
    left: -125px;
    z-index: 5
  }

  @media screen and (max-height: 768px) {
    .sidebar_child_top {
      transform: translateY(calc(-100% + 42px));
    }
  }

  .location_ico {
    display: inline-block;
    float: left;
    width: 30px;
    margin-left: 10px;
    height: 36px;
    background: url(/static/imgs/icons/location_ico.png) no-repeat left 4px
  }

  .body_top {
    border-bottom: 1px solid #DDDDDD;
    height: 36px;
    padding-top: 12px;
  }

  .body_breadcrumb {
    height: 36px;
    line-height: 30px;
    border-bottom: 2px solid #BD122C;
    float: left;
    margin-bottom: -1px;
    margin-right: 60px;
    padding-right: 20px;
  }

  .body_nav li {
    float: left;
    margin-bottom: -1px;
    margin-right: 10px;
    text-align: center;
  }

  .body_nav li a {
    display: inline-block;
    height: 36px;
    line-height: 28px;
    margin-bottom: -1px;
    text-align: center;
    text-decoration: none;
    color: #000;
    font-size: 14px;
    padding: 0 20px;
  }

  .body_nav .body_nav_active a {
    border-bottom: 2px solid #C8384B;
    color: #C8384B;
  }

</style>
